package com.ghy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ghy.dto.CategoryInsertDTO;
import com.ghy.dto.CategoryPageDTO;
import com.ghy.dto.CategoryUpdateDTO;
import com.ghy.exception.ServiceException;
import com.ghy.vo.CategorySimpleListVO;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.ghy.entity.Category;
import com.ghy.mapper.CategoryMapper;
import com.ghy.service.CategoryService;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

import static com.ghy.entity.table.CategoryTableDef.CATEGORY;
import static com.ghy.entity.table.UserTableDef.USER;

/**
 * 类别表 服务层实现。
 *
 * @author ghy
 * @since 2024-12-22
 */
@Service
@CacheConfig(cacheNames = "category")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>  implements CategoryService{


    @CacheEvict(allEntries = true)
    @Override
    public boolean insert(CategoryInsertDTO dto) {

        // 标题查重
        if (QueryChain.of(mapper)
                .where(CATEGORY.TITLE.eq(dto.getTitle()))
                .exists()){
            throw new ServiceException("类别标题已存在");
        }

         // 组装 entity 实体类
        Category category = BeanUtil.copyProperties(dto, Category.class);
        category.setCreated(LocalDateTime.now());
        category.setUpdated(LocalDateTime.now());

        return mapper.insert(category) > 0;
    }

    @Cacheable(key ="#p0",condition="#p0 != null",unless = "#result == null")
    @Override
    public Category select(Long id) {
        return mapper.selectOneWithRelationsById(id);
    }


    @Cacheable(key = "#root.methodName + ':' + #p0.toString()",
            condition="#p0 != null",
            unless = "#result == null")
    @Override
    public Page<Category> page(CategoryPageDTO dto) {
        QueryChain<Category> queryChain = QueryChain.of(mapper)
                .orderBy(CATEGORY.IDX.asc(), CATEGORY.ID.desc());

        // title 条件
        String title = dto.getTitle();
        if(ObjectUtil.isNotNull(title)){
            queryChain.where(CATEGORY.TITLE.like(title));
        }
        return queryChain.withRelations()
                .page(new Page<>(dto.getPageNum(), dto.getPageSize()));

    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean update(CategoryUpdateDTO dto) {
        //标题查重
        if (QueryChain.of(mapper)
                .where(CATEGORY.TITLE.eq(dto.getTitle()))
                .and(CATEGORY.ID.ne(dto.getId()))
                .exists()){
            throw new ServiceException("类别标题已存在“");
        }

        // 组装 entity 实体类
        Category category = BeanUtil.copyProperties(dto, Category.class);
        category.setUpdated(LocalDateTime.now());

        return UpdateChain.of(category)
                .where(CATEGORY.ID.eq(category.getId()))
                .update();
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean delete(Long id) {
        return mapper.deleteById(id) > 0;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean deleteBatch(List<Long> ids) {
        return mapper.deleteBatchByIds(ids) >0;
    }

    @Override
    @Cacheable(key = "#root.methodName",
            unless ="#result == null")
    public List<CategorySimpleListVO> simpleList(){
        return QueryChain.of(mapper)
                .orderBy( CATEGORY.ID.asc())
                .withRelations()
                .listAs(CategorySimpleListVO.class);
    }


}
